﻿using System;
using System.IO;
using System.Linq;

using Calais;

namespace CalaisDotNetConsoleTests
{
    class CalaisTests
    {
        private static string _apiKey;
        private static string _content;
        private static string _preProcessed;

        static void Main(string[] args)
        {
            // read API key from file on desktop
            string apiKeyPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Desktop\CalaisApiKey.txt";

            _apiKey = File.ReadAllText(apiKeyPath);
            _content = File.ReadAllText("CalaisTest.txt");

            var calais = new CalaisDotNet(_apiKey, _content);
            var document = calais.Call<CalaisJsonDocument>();

            Console.WriteLine(document.RawOutput);


            //_preProcessed = File.ReadAllText("CalaisPreProcessed.xml");

            //var document = CalaisDotNet.Parse<CalaisJsonDocument>(_preProcessed);
            //Console.WriteLine(document.RawOutput);

            //Console.WriteLine((CalaisSimpleEventType) StringEnum.Parse(typeof (CalaisSimpleEventType), "M&A"));

            //var calais = new CalaisDotNet(_apiKey, _content);
            //var document = calais.Call<CalaisMicroFormatsDocument>();

            //Console.WriteLine(document.RawOutput);

            //Console.WriteLine();
            //Console.WriteLine("----[ RDF ]----");
            //Console.WriteLine();

            //var calais = new CalaisDotNet(_apiKey, _content);
            //var document = calais.Call<CalaisRdfDocument>();

            //Console.WriteLine();
            //Console.WriteLine("----[ Description (ContentType) ]----");
            //Console.WriteLine();

            //Console.WriteLine("Description:" + document.Description.ContentType);

            //Console.WriteLine();
            //Console.WriteLine("----[ Entity Query (Type == Country) ]----");
            //Console.WriteLine();

            //var results = from item in document.Entities
            //              //where item.EntityType == CalaisRdfEntityType.Country
            //              select item;

            //foreach (var result in results)
            //{
            //    Console.Write(result);
            //    Console.WriteLine(" (Freq: " + result.Instances.Count() +")");
            //}

            //Console.WriteLine();
            //Console.WriteLine("----[ Relationships ]----");
            //Console.WriteLine();

            //var moreResults = from item in document.Relationships
            //                  select item;

            //foreach (var result in moreResults)
            //{
            //    Console.Write(result.RelationshipType + ": " + result);
            //    Console.WriteLine(" (Freq: " + result.Instances.Count() + ")");
            //}

            //Console.WriteLine("----[ RAW OUTPUT ]----");
            //File.WriteAllText("CalaisOutput.txt", document.RawOutput);
            //Console.WriteLine(document.RawOutput);

            //Console.WriteLine();
            //Console.Read();

            //Console.WriteLine();
            //Console.WriteLine("----[ SIMPLE ]----");
            //Console.WriteLine();

            //var calais = new CalaisDotNet(_apiKey, _content, CalaisInputFormat.Text, "http://base.url.org", true, true, "MyExternId", "MySubmitterId", false, true);
            //var document = calais.Call<CalaisSimpleDocument>();

            //Console.WriteLine("----[ RAW OUTPUT ]----");

            //Console.WriteLine(document.RawOutput);

            //Console.WriteLine();
            //Console.WriteLine("----[ PRINTING ALL ]----");

            //foreach (var entity in document.Entities)
            //{
            //    Console.WriteLine(entity.Value + " / " + entity.Type + " / " + entity.Frequency + " / " + entity.Relevance);
            //}

            //Console.WriteLine();
            //Console.WriteLine("----[ SORTING - By Relevance ]----");

            //var sorted = from item in document.Entities
            //             orderby item.Relevance descending 
            //             select item;

            //foreach (var entity in sorted)
            //{
            //    Console.WriteLine(entity.Value + " / " + entity.Type + " / " + entity.Frequency + " / " + entity.Relevance);    
            //}

            //Console.WriteLine();
            //Console.WriteLine("----[ PRINTING QUERY - Type == Country ]----");

            //var results = from item in document.Entities
            //              where item.Type == CalaisSimpleEntityType.Country
            //              select item;

            //foreach (var e in results)
            //{
            //    Console.WriteLine(e.Value + " / " + e.Type + " / " + e.Frequency);
            //}

            //Console.WriteLine();
            //Console.WriteLine("----[ PRINTING QUERY - Value LIKE 'Adobe' ]----");

            //var results2 = from item in document.Entities
            //               where item.Value.Contains("Adobe")
            //               select item;

            //foreach (var e in results2)
            //{
            //    Console.WriteLine(e.Value + " / " + e.Type + " / " + e.Frequency);
            //}

            Console.WriteLine();
            Console.Read();
        }
    }
}
